/**
 * @description  集中度管理
 * @author zls
 */
import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { Spin } from 'antd';
import { SecondHeaderC, TableC, FirstHeaderC } from '../../../comps';
import ketou from '../../../assets/busi/reportingServices/tick_icon.png';
import buketou from '../../../assets/busi/reportingServices/cross_icon.png';
import Style from './style.less';

class ConcentrationManagement extends PureComponent {
  constructor() {
    super();
    this.state = {
      data: [{}, {}, {}],
    };
  }

  componentDidMount() {
    const { searchData } = this.props;
    this.getData(searchData);
  }

  UNSAFE_componentWillReceiveProps(nextProps) {
    const { searchData } = nextProps;

    if (JSON.stringify(searchData) !== JSON.stringify(this.props.searchData)) {
      this.getData(searchData);
    }
  }

  //获取数据
  getData = (payload) => {
    this.props.dispatch({
      type: 'commercialBankReportM/getConcentrationManagement',
      payload: {
        ...payload,
      },
      callback: (data) => {
        if (data) {
          this.setState({
            data: data,
          });
        } else {
          this.setState({
            data: [{}, {}, {}],
          });
        }
      },
    });
  };

  render() {
    const { data } = this.state;
    const { loading } = this.props;

    // 和现金报告那不一样，这里是1禁止0通过
    const iconInfo = {
      1: {
        className: 'prohibit',
        img: buketou,
        name: '禁止',
      },
      0: {
        className: 'adopt',
        img: ketou,
        name: '通过',
      },
    };
    return (
      <>
        <div name={'syyh-pdf'} style={{ marginBottom: '16px' }}>
          <FirstHeaderC title='集中度管理' bottomHeight={false} />
        </div>

        {/* <Spin spinning={this.props.loading == true}> */}

        {data.map((r, index) => {
          return (
            <div key={index}>
              {r.msg && (
                <div name={'syyh-pdf'} style={{ marginBottom: '16px' }}>
                  <SecondHeaderC
                    title={r.msg}
                    bottomHeight={false}
                    Left={
                      <div className={Style[iconInfo[r.state]?.className]} style={{ marginLeft: '20px' }}>
                        <img src={iconInfo[r.state]?.img} />
                        {iconInfo[r.state]?.name}
                      </div>
                    }
                  />
                </div>
              )}
              {(r?.dataSource?.length != 0 || loading) && (
                <div name={'syyh-pdf'} isTable='true' style={{ marginBottom: '16px' }}>
                  <TableC
                    columns={
                      r.columns
                        ?.map((x) => {
                          if (x.key == 'productName') {
                            return {
                              ...x,
                              width: '300px',
                            };
                          }

                          if (x.key == 'amount') {
                            return {
                              ...x,
                              type: 'number',
                            };
                          }
                          if (x.key == 'value') {
                            return {
                              ...x,
                              type: 'money',
                            };
                          }
                          if (x.key == 'weight') {
                            return {
                              ...x,
                              align: 'right',
                            };
                          }

                          return x;
                        })
                        ?.concat([{ dataIndex: 'konglie', title: '', key: 'konglie', render: () => '' }]) //添加空列，解决表头导出的样式问题
                    }
                    dataSource={r.dataSource}
                    hidePage={true}
                    rowKey={'id'}
                    loading={loading}
                    rowClassName={'table-row'}
                  ></TableC>
                </div>
              )}
            </div>
          );
        })}
        {/* </Spin> */}
      </>
    );
  }
}
export default connect(({ commercialBankReportM, loading }) => ({
  commercialBankReportM,
  loading: loading.effects['commercialBankReportM/getConcentrationManagement'],
}))(ConcentrationManagement);
